package com.ssyx.acl.controller;

import com.ssyx.acl.service.PermissionService;
import com.ssyx.common.result.Result;
import com.ssyx.model.acl.Permission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName:PermissionAdminController
 * Description:
 *
 * @Author kay
 * @Create 2024/4/24 23:23
 * @Version 1.0
 */
@Tag(name = "菜单管理接口")
@RestController
@RequestMapping("/admin/acl/permission")
//@CrossOrigin
public class PermissionAdminController {

    @Autowired
    private PermissionService permissionService;

    @Operation(summary = "获取菜单")
    @GetMapping
    public Result index(){
        List<Permission> list = permissionService.getAllMenu();
        return Result.ok(list);
    }

    @Operation(summary = "新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody Permission permission){
        permissionService.save(permission);
        return Result.ok(null);
    }

    @Operation(summary = "修改菜单")
    @PutMapping("update")
    public Result update(@RequestBody Permission permission){
        permissionService.updateById(permission);
        return Result.ok(null);
    }

    @Operation(summary = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        permissionService.removeChildById(id);
        return Result.ok(null);
    }

    @Operation(summary = "根据角色id获取菜单信息")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId){
        //返回List集合:所有菜单,并且已分配过的菜单要选中
        List<Permission> permissionList = permissionService.getPermissionByRoleId(roleId);
        return Result.ok(permissionList);

    }

    @Operation(summary = "给指定角色分配菜单权限")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long roleId,
                           @RequestParam Long[] permissionIds){
        permissionService.saveRolePermission(roleId,permissionIds);
        return Result.ok(null);
    }

}
